﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AutomaticInterferenceVolumeCreation.CApAIVCInterferenceZonesCreator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.InterferenceVolume;
using DnProcessSimulateCommands.SweptVolume;
using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.AutomaticInterferenceVolumeCreation;

internal class CApAIVCInterferenceZonesCreator
{
  private CApAIVCInterferenceZone m_izCreator;
  private CApAIVCLogMessageEventHandler m_logMessage;
  private bool m_shouldStop;

  internal event NextMidLevelStepStartedEventHandler NextMidLevelStep;

  internal event LowLevelStepDurationEventHandler LowLevelStepDuration;

  internal event LowLevelStepProgressEventHandler LowLevelStepProgress;

  internal event CApSimulationHandler_SimulationInitializedEventHandler SimulationInitialized;

  public CApAIVCLogMessageEventHandler LogMessage
  {
    set => this.m_logMessage = value;
  }

  public CApAIVCInterferenceZonesCreator()
  {
    this.m_izCreator = (CApAIVCInterferenceZone) null;
    this.m_shouldStop = false;
  }

  public void InterferenceZoneCreation(TxObjectList interferenceVolumes, bool includeCables)
  {
    int num = ((Collection<ITxObject>) interferenceVolumes).Count * 2;
    foreach (TxInterferenceVolume interferenceVolume in (Collection<ITxObject>) interferenceVolumes)
    {
      List<ITxInterferenceVolumeHoldingObject> volumeHoldingObjects = CApIVUtilities.GetInterferenceVolumeHoldingObjects(interferenceVolume);
      for (int index = 0; index < volumeHoldingObjects.Count && !this.m_shouldStop; ++index)
      {
        ITxInterferenceVolumeHoldingObject holdingObject = volumeHoldingObjects[index];
        this.FireMidLevelProgress(num);
        this.FireLowLevelDuration(this.GetSimulationDuration(holdingObject));
        this.RunSingleInterferenceZone(holdingObject, interferenceVolume, includeCables);
      }
      TxCollisionRootEx.CollisionClearVolume((ITxObject) interferenceVolume);
      interferenceVolume.Blank();
    }
    this.FireLogMessage(num);
  }

  public void AbortCreation()
  {
    this.m_shouldStop = true;
    this.m_izCreator = (CApAIVCInterferenceZone) null;
  }

  private double GetSimulationDuration(ITxInterferenceVolumeHoldingObject holdingObject)
  {
    double simulationDuration = 0.0;
    switch (holdingObject)
    {
      case ITxRoboticOrderedCompoundOperation _:
        simulationDuration = ((ITxOperation) (holdingObject as ITxRoboticOrderedCompoundOperation)).Duration;
        break;
      case ITxRoboticProgram _:
        simulationDuration = (holdingObject as ITxRoboticProgram).Duration;
        break;
    }
    return simulationDuration;
  }

  private void RunSingleInterferenceZone(
    ITxInterferenceVolumeHoldingObject holdingObject,
    TxInterferenceVolume interferenceVolume,
    bool includeCables)
  {
    this.m_izCreator = new CApAIVCInterferenceZone();
    this.m_izCreator.SimulationInitialized += new CApSimulationHandler_SimulationInitializedEventHandler(this.m_izCreator_SimulationInitialized);
    this.m_izCreator.LowLevelStepProgress += new LowLevelStepProgressEventHandler(this.OnLowLevelProgress);
    this.m_izCreator.sampleInterferenceZone(holdingObject as ITxSweptVolumeHoldingObject, (ITxLocatableObject) interferenceVolume, includeCables);
    List<TxInterferenceVolumeLocationDataEx> volumeLocationData = this.CreateInteferenceVolumeLocationData(interferenceVolume, this.m_izCreator.InterferenceZones);
    TxInterferenceVolumeEx.SetHoldingObjectInterferenceLocations(interferenceVolume, holdingObject, volumeLocationData);
    this.m_izCreator.LowLevelStepProgress -= new LowLevelStepProgressEventHandler(this.OnLowLevelProgress);
  }

  private List<TxInterferenceVolumeLocationDataEx> CreateInteferenceVolumeLocationData(
    TxInterferenceVolume interferenceVolume,
    CApSVInterferenceZoneData[] interferenceZones)
  {
    List<TxInterferenceVolumeLocationDataEx> volumeLocationData1 = new List<TxInterferenceVolumeLocationDataEx>();
    if (interferenceZones != null)
    {
      for (int index = 0; index < interferenceZones.Length; ++index)
      {
        CApSVInterferenceZoneData interferenceZone = interferenceZones[index];
        TxInterferenceVolumeLocationDataEx volumeLocationData2 = this.m_izCreator.CreateInterferenceVolumeLocationData(interferenceVolume, interferenceZone.Enter);
        TxInterferenceVolumeLocationDataEx volumeLocationData3 = this.m_izCreator.CreateInterferenceVolumeLocationData(interferenceVolume, interferenceZone.Exit);
        volumeLocationData1.Add(volumeLocationData2);
        volumeLocationData1.Add(volumeLocationData3);
      }
    }
    return volumeLocationData1;
  }

  private void FireMidLevelProgress(int nSimulations)
  {
    if (this.NextMidLevelStep == null)
      return;
    this.NextMidLevelStep((object) this, new NextMidLevelStepStartedEventArgs(nSimulations));
  }

  private void FireLowLevelDuration(double duration)
  {
    if (this.LowLevelStepDuration == null)
      return;
    this.LowLevelStepDuration((object) this, new LowLevelStepDurationEventArgs(duration));
  }

  private void OnLowLevelProgress(object sender, EventArgs e)
  {
    if (this.LowLevelStepProgress == null)
      return;
    this.LowLevelStepProgress(sender, e);
  }

  private void FireLogMessage(int numInterfereceZones)
  {
    if (this.m_logMessage == null)
      return;
    this.m_logMessage((object) this, new CApAIVCLogMessageEventArgs(new CApAIVCLogMessage(eApAIVCLogCode.INTERFERENCE_ZONE, numInterfereceZones)));
  }

  private void m_izCreator_SimulationInitialized(object sender, EventArgs e)
  {
    if (this.SimulationInitialized == null)
      return;
    this.SimulationInitialized(sender, e);
  }
}
